package com.xxx.video.commen.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import com.xxx.video.R;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.presenters.ICommonBaseView;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.github.inflationx.viewpump.ViewPumpContextWrapper;

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements ICommonBaseView {

    private Unbinder unbinder;
    /**
     * this is a root view
     */
    private ViewAnimator mViewAnimator;
    /**
     * presenter
     */
    private P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
         * is open eventBus
         */
        if (isOpenEventBus()) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
        /*
         * init layout
         */
        initLayout();
        /*
         * init actionbar layout
         */
        initActionBar();
        /*
         * butterKnife
         */
        unbinder = ButterKnife.bind(this);
        /*
         * bind presenter(onDestroy must unbind)
         */
        bindPresenter();
        /*
         * init Data
         */
        initData(getIntent().getExtras());
        /*
         * initListener
         */
        initListener();
        /*
         * init toolBar
         */
        initToolbarData();
    }

    /**
     * init listener
     */
    protected void initListener() {

    }

    @Override
    public void initToolbarData() {

    }

    /**
     * init Actionbar
     */
    protected void initActionBar() {

    }

    /**
     * init layout
     */
    private void initLayout() {

        /*
         * get inflater
         */
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (null == inflater) {
            return;
        }
        View mContextView = inflater.inflate(R.layout.activity_base, null, false);
        mViewAnimator = mContextView.findViewById(R.id.activity_base);
        /*
         * loading view
         */
        inflater.inflate(getLoadingLayout(), mViewAnimator, true);
        /*
         * content view
         */
        inflater.inflate(getContentLayout(), mViewAnimator, true);
        /*
         * empty data view
         */
        inflater.inflate(getNoDataLayout(), mViewAnimator, true);
        /*
         * load data error view
         */
        inflater.inflate(getErrorLayout(), mViewAnimator, true);
        setContentView(mContextView);
    }


    @Override
    public int getErrorLayout() {
        return R.layout.common_error_layout;
    }


    @Override
    public int getNoDataLayout() {
        return R.layout.common_no_data_layout;
    }


    @Override
    public int getLoadingLayout() {
        return R.layout.common_loading_layout;
    }

    @Override
    public boolean isOpenEventBus() {
        return false;
    }

    @Override
    public boolean isUseMultiLayerLayout() {
        return true;
    }

    @Override
    public void bindPresenter() {
        if (null == presenter) {
            presenter = createPresenter();
        }
    }

    @Override
    public void unBindPresenter() {
        if (null != getPresenter()) {
            getPresenter().unBindPresenter();
        }
    }

    /**
     * create presenter
     *
     * @return presenter
     */
    protected P createPresenter() {
        return null;
    }

    /**
     * get a presenter
     *
     * @return presenter
     */
    protected P getPresenter() {
        return presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /*
         * is open eventBus
         */
        if (isOpenEventBus()) {
            if (EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }
        }
        unbinder.unbind();
        unBindPresenter();
    }

    @Override
    public void intent2Activity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    @Override
    public void intent2Activity(Class clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void intent2Activity(Class clazz, int requestCode) {
        Intent intent = new Intent(this, clazz);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void intent2Activity(Class clazz, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void showFragment(Fragment fragment) {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }

    @Override
    public void hideFragment(Fragment fragment) {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragment);
        transaction.commit();
    }

    @Override
    public void addFragment(int containerViewId, Fragment fragment, @Nullable String tag) {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(containerViewId, fragment, tag);
        transaction.commit();
    }

    @Override
    public void replaceFragment(int containerViewId, Fragment fragment) {
        FragmentManager fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(containerViewId, fragment);
        transaction.commit();
    }

    /**
     * hide actionbar
     */
    protected void hideActionBar() {
        ConstraintLayout actionbar = findViewById(R.id.cl_actionbar);
        if (actionbar != null) {
            actionbar.setVisibility(View.GONE);
        }
    }

    /**
     * show actionbar
     */
    protected void showActionBar() {
        ConstraintLayout actionbar = findViewById(R.id.cl_actionbar);
        if (actionbar != null) {
            actionbar.setVisibility(View.VISIBLE);
            setActionBarMarginTop(StatusBarUtil.getInstance().getStatusBarHeight(this));
        }
    }

    /**
     * set actionBar margin top
     *
     * @param marginTop px
     */
    public void setActionBarMarginTop(int marginTop) {
        ConstraintLayout actionbar = findViewById(R.id.cl_actionbar);
        if (actionbar != null) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) actionbar.getLayoutParams();
            layoutParams.setMargins(layoutParams.leftMargin, marginTop, layoutParams.rightMargin, layoutParams.bottomMargin);
        }
    }


    /**
     * set is show actionbar
     *
     * @param isShow is show
     */
    public void setIsShowActionbarLine(boolean isShow) {
        View vActionbarBottomLine = findViewById(R.id.v_actionbar_bottom_line);
        if (vActionbarBottomLine != null) {
            vActionbarBottomLine.setVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void showLayoutStatus(int showState) {
        if (mViewAnimator == null) {
            return;
        }
        int displayedChild = mViewAnimator.getDisplayedChild();
        if (displayedChild == showState) {
            return;
        }
        mViewAnimator.setDisplayedChild(showState);
    }

    @OnClick({R.id.iv_actionbar_left_icon, R.id.tv_actionbar_left_tile, R.id.iv_actionbar_right_icon, R.id.tv_actionbar_right_title, R.id.tv_common_error_layout_refresh})
    public void onActionBarViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_actionbar_left_icon:
            case R.id.tv_actionbar_left_tile:
                onBackPressed();
                break;
            case R.id.iv_actionbar_right_icon:
            case R.id.tv_actionbar_right_title:
                onActionBarRightIconClick();
                break;
            case R.id.tv_common_error_layout_refresh:
                showLayoutStatus(0);
                initData(getIntent().getExtras());
                refreshData(getIntent().getExtras());
                break;
            default:
                break;
        }
    }

    /**
     * actionbar right icon click
     */
    public void onActionBarRightIconClick() {

    }

    /**
     * setActionbar right icon
     *
     * @param resid res id
     */
    public void setActionBarRightIcon(@DrawableRes int resid) {
        ImageView tvActionbarRightIcon = findViewById(R.id.iv_actionbar_right_icon);
        if (tvActionbarRightIcon != null) {
            tvActionbarRightIcon.setVisibility(View.VISIBLE);
            tvActionbarRightIcon.setImageResource(resid);
        }
    }

    /**
     * setActionbar right  title
     *
     * @param resid title
     */
    public void setActionBarRightTitle(@StringRes int resid, @ColorRes int colorResId) {
        TextView tvActionbarRightTitle = findViewById(R.id.tv_actionbar_right_title);
        if (tvActionbarRightTitle != null) {
            tvActionbarRightTitle.setVisibility(View.VISIBLE);
            tvActionbarRightTitle.setText(resid);
            tvActionbarRightTitle.setTextColor(ResourcesUtils.getInstance().getColor(colorResId));
        }
    }

    /**
     * set Title
     *
     * @param resid title
     */
    public void setActionBarTitle(@StringRes int resid) {
        TextView tvActionbarLeftTile = findViewById(R.id.tv_actionbar_left_tile);
        if (tvActionbarLeftTile != null) {
            tvActionbarLeftTile.setText(resid);
        }
    }

    /**
     * set Title
     *
     * @param text title
     */
    public void setActionBarTitle(CharSequence text) {
        TextView tvActionbarLeftTile = findViewById(R.id.tv_actionbar_left_tile);
        if (tvActionbarLeftTile != null) {
            tvActionbarLeftTile.setText(text);
        }
    }

    /**
     * setActionbar left icon
     *
     * @param resid res id
     */
    public void setActionBarLeftIcon(@DrawableRes int resid) {
        ImageView tvActionbarRightIcon = findViewById(R.id.iv_actionbar_left_icon);
        if (tvActionbarRightIcon != null) {
            tvActionbarRightIcon.setVisibility(View.VISIBLE);
            tvActionbarRightIcon.setImageResource(resid);
        }
    }

    /**
     * set widow alpha
     */
    public void lighton() {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 1.0f;
        getWindow().setAttributes(lp);
    }

    /**
     * set window alpha
     */
    protected void lightof() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.8f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
    }
    protected void hideKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
    @Override
    public void refreshData(Bundle extras) {

    }
}
